﻿
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Report.Forms
{
    /// <summary>
    /// FrmRemoveCurve.xaml 的交互逻辑
    /// </summary>
    public partial class FrmRemoveResultCurve : Window
    {
        private readonly MultiavityReportManager1 _manager;
        private readonly ObservableCollection<CurveListItem> _curves;
        private bool _isRemove;
        public FrmRemoveResultCurve()
        {
            InitializeComponent();
            _manager = MultiavityReportManager1.Create();
            _curves = new ObservableCollection<CurveListItem>();
        }

        private void FrmRemoveCurve_OnLoaded(object sender, RoutedEventArgs e)
        {
            _initCurves();
        }

        private void _initCurves()
        {
            _curves.Clear();
            foreach (var curve in _manager.Curves)
            {
                var cli = new CurveListItem
                {
                    Name = curve.Title,
                    IsShowPoint = curve.IsDrawPoint
                };
                var fd = _manager.ResultInfo.SingleOrDefault(d => d.DataNo == curve.X.Id);
                if (fd != null)
                {
                    cli.XData = fd.Text;
                }
                var str = new StringBuilder();
                foreach (var ys in curve.Y)
                {
                    fd = _manager.ResultInfo.SingleOrDefault(d => d.DataNo == ys.Id);
                    if (fd != null)
                    {
                        str.Append(fd.Text).Append(",");
                    }
                }
                cli.YDatas = str.Remove(str.Length - 1, 1).ToString();
                _curves.Add(cli);
            }
            Curves.ItemsSource = _curves;
        }

        private void BtnRemove_OnClick(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            var title = btn?.Tag.ToString();
            if (_manager.RemoveCurve(title) > 0)
            {
                _isRemove = true;
                var cli = _curves.SingleOrDefault(c => c.Name == title);
                if (cli != null) _curves.Remove(cli);
            }
        }

        private void BtnOk_OnClick(object sender, RoutedEventArgs e)
        {
            DialogResult = _isRemove;
            Close();
        }
    }
    
}
